package com.zx.Servlet;

import com.zx.Dao.Jdbc;
import com.zx.Moudel.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * com.zx.Servlet
 * 26832
 * zx
 * &#064;description:
 * &#064;author: lxm
 * Register
 * &#064;create: 2024-06-24 09:32
 **/
@WebServlet("/register")
public class Register extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String uname = req.getParameter("name");
        String upwd = req.getParameter("password");
        String phone = req.getParameter("phone");
        //数据库
        String sql = "insert into users(uname, password, phone,role) values (?,?,?,?)";
        Connection con = null;
        PreparedStatement psmt = null;
        int i =-1;
        Jdbc jdbc = new Jdbc();
        try{
            con = jdbc.getCon();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,uname);
            psmt.setString(2,upwd);
            psmt.setString(3,phone);
            psmt.setString(4,"common");
            i = psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            jdbc.Close(con,null,null);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if(i>0){
            resp.getWriter().write("success");
        }else{
            resp.getWriter().write("faild");
        }

    }
}
